
package com.scalar.chiptrack.sales.actions;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.chiptrack.sales.forms.OrdersForm;


/**
 * Implementation of <strong>Action</strong> that gives customers list,
 * search with customer name, create sales order
 *
 * @author Kesava Kumar Achanala
 * @version $Revision: 1.1.1.1 $ $Date: 2006/12/11 23:43:18 $
 */

public final class SearchChangeOrderAction extends ChipTrackAction {


    // ----------------------------------------------------- Instance Variables
        ArrayList pendingList  = new ArrayList();

    // --------------------------------------------------------- Public Methods


    /**
     * Process the specified HTTP request, and create the corresponding HTTP
     * response (or forward to another web component that will create it).
     * Return an <code>ActionForward</code> instance describing where and how
     * control should be forwarded, or <code>null</code> if the response has
     * already been completed.
     *
     * @param mapping The ActionMapping used to select this instance
     * @param form The optional ActionForm bean for this request (if any)
     * @param request The HTTP request we are processing
     * @param response The HTTP response we are creating
     *
     * @exception Exception if business logic throws an exception
     */
    public ActionForward execute(ActionMapping mapping,
				 ActionForm form,
				 HttpServletRequest request,
				 HttpServletResponse response)
	throws Exception {

		// Extract attributes and parameters we will need
		OrdersForm subform = (OrdersForm) form;
		String action = request.getParameter("action");
		
		// Validate the request parameters specified by the user
		ActionErrors errors = new ActionErrors();
		
                if(action == null)
                    request.setAttribute("showList", "FALSE"); //Not to show the list by default
                               
                if("SearchOrder".equals(action)) {
			//here using the property utils we can get the form value
			String customerId = (String)
				BeanUtils.getSimpleProperty(form, "customerId");
			String marketingPartNo = (String)
				BeanUtils.getSimpleProperty(form, "marketingPartNo");
                        String fromDate = (String)BeanUtils.getSimpleProperty(form, "fromDate");
                        String toDate = (String)BeanUtils.getSimpleProperty(form, "toDate");
                        
                        String salesOrderId = (String)BeanUtils.getSimpleProperty(form,"salesOrderId");

                        if((salesOrderId != null) && (salesOrderId.trim().length() != 0)) {
                            //pendingList = SalesDAO.loadChangeOrdersWithSalesOrderId(salesOrderId);
                            //Add errors if pendinglist is null ?????
                                
                        } else {
                            //pendingList = SalesDAO.loadChangeOrdersWithoutDates(customerId,marketingPartNo);
                        }
                        
                        //Show the pendingList only when it searched
                        request.setAttribute("showList", "TRUE");
                                
		}
                
                request.setAttribute("customerList",CustomerDAO.getAllCustomerNames());
                request.setAttribute("pendingList", pendingList);
                
		// Report any errors we have discovered back to the original form
		if (!errors.empty()) {
			saveErrors(request, errors);
			return (new ActionForward(mapping.getInput()));
		}

                                
                //Please look at this return later(Where do we need this retrurned) ????
		return (mapping.findForward("success"));

    }

}
